home *** CD-ROM | disk | FTP | other *** search
- program TestDate2Str;
-
- uses
- SysUtils,
- Dialogs,
- Str2Date;
-
- var
- bDayB4Month:boolean;
- TotalTests: integer;
- TotalFails: integer;
-
- procedure ExtractDate(const DateValue: TDateTime; const DateText: string; const DayB4Month: boolean);
- begin
- try
- Inc(TotalTests);
- if DateValue <> StrToDateNew(DateText, DayB4Month) then
- Raise Exception.CreateFmt('Incorrect QDate from Str2Date(%s, ..)', [DateText]);
- except
- on E: Exception do
- begin
- MessageDlg(E.Message, mtError, [mbOk], 0);
- Inc(TotalFails);
- end;
- end;
- end;
-
- procedure FunctionalTests;
- var
- DateText: string;
- DateValue: TDateTime;
- iYear, iMonth, iDay: integer;
- begin
-
- ExtractDate(3, '1900\1\2', False); // YMD
- ExtractDate(3, '1900/2/1', True); // YDM
- ExtractDate(3, '2-1-1900', True); // DMY
- ExtractDate(3, '1.2.1900', False); // MDY
-
- ExtractDate(3, ' 1900 / 1 / 2 ', False); // YMD and multiple separators
- ExtractDate(3, ' 1900 / 2 / 1 ', True); // YDM and multiple separators
- ExtractDate(3, ' 2 \ 1 \ 1900 ', True); // DMY and multiple separators
- ExtractDate(3, ' 1 \ 2 \ 1900 ', False); // MDY and multiple separators
-
- ExtractDate(3, ' 1900 2 Jan ', True); // YDM Month Name and multiple separators
- ExtractDate(3, ' 1900 Jan 2 ', False); // YMD Month Name and multiple separators
- ExtractDate(3, ' 2 Jan 1900 ', True); // DMY Month Name and multiple separators
- ExtractDate(3, ' Jan 2 1900 ', False); // MDY Month Name and multiple separators
-
- for iYear := 1900 to 2200 do
- for iMonth := 1 to 12 do
- for iDay := 1 to 2 do
- begin
- DateValue := EncodeDate(iYear, iMonth, iDay);
- DateText := DateToStr(DateValue);
- ExtractDate(DateValue, DateText, bDayB4Month);
- end;
- end;
-
- procedure PerformanceTest;
- var
- iLoop: integer;
- Factor: double;
- DateText: string;
- BegTime, EndTime: TDateTime;
- DelphiElapsed, HelperElapsed: TDateTime;
- const
- LoopMax = 1000000;
- begin
-
- DateText := DateToStr(EncodeDate(2000, 1, 1));
-
- BegTime := Time;
- for iLoop := 1 to LoopMax do
- StrToDate(DateText);
- EndTime := Time;
- DelphiElapsed := EndTime - BegTime;
-
- BegTime := Time;
- for iLoop := 1 to LoopMax do
- StrToDateNew(DateText, bDayB4Month);
- EndTime := Time;
- HelperElapsed := EndTime - BegTime;
-
- Factor := DelphiElapsed / HelperElapsed;
- DateText := Format('StrToDateNew was %2.2g times' +
- #10'faster than StrToDate!', [Factor]);
- MessageDlg(DateText, mtInformation, [mbOk], 0);
- end;
-
- begin
- TotalTests := 0;
- TotalFails := 0;
-
- bDayB4Month := True;
- // Delphi date/string routines hate short dates with a month name.
- ShortDateFormat := 'dd' + DateSeparator + 'mm' + DateSeparator + 'yyyy';
-
- FunctionalTests;
-
- MessageDlg(IntToStr(TotalTests) + ' Tests Performed', mtInformation, [mbOk], 0);
- MessageDlg(IntToStr(TotalFails) + ' Failures', mtWarning, [mbOk], 0);
-
- if TotalFails = 0 then PerformanceTest;
-
- end.
-
-